package zjz.graduation.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zjz.graduation.backend.dto.GeneralMessage;
import zjz.graduation.backend.dto.LoginMessage;
import zjz.graduation.backend.model.Users;
import zjz.graduation.backend.service.AdminsService;
import zjz.graduation.backend.service.UserService;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UsersController {
    @Autowired
    UserService userService;
    @Resource
    AdminsService adminsService;

    //等价@PostMapping("/login")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public LoginMessage login(@RequestBody Users users){
        LoginMessage message = new LoginMessage();
        Users sqlUsers = userService.getByUsername(users.getUsername());
        if(sqlUsers !=null&& users.getPassword().equals(sqlUsers.getPassword())){
            message.setState(true);
            message.setMessage(sqlUsers.getUid());
            if(adminsService.isExist(sqlUsers.getUid())){
                message.setRole(true);
            }
            message.setMessage(sqlUsers.getUid());
        }
        return message;
    }

    @RequestMapping(value = "/getAdmins",method = RequestMethod.GET)
    public GeneralMessage getAdmins() {
    	GeneralMessage message = new GeneralMessage();
    	message.setMessage(adminsService.getAdmins());
    	return message;
    }
    
    @RequestMapping(value = "/getUsers",method = RequestMethod.GET)
    public GeneralMessage getUsers() {
    	GeneralMessage message = new GeneralMessage();
    	message.setMessage(userService.getUsers());
    	return message;
    }

    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public GeneralMessage updateRole(@RequestBody Map map) {
        adminsService.updateRole((Integer) map.get("uid"), (Integer) map.get("role"));
        return new GeneralMessage();
    }
}
